//
//  UserBaseViewController.swift
//  jiaoyi
//
//  Created by wjk on 2018/8/9.
//  Copyright © 2018年 wjk. All rights reserved.
//

import UIKit
import SwiftyJSON

class UserBaseViewController: BaseViewController,UITableViewDataSource,UITableViewDelegate {

    @IBOutlet weak var labelNote: UILabel!
    
    var enterprise_id:Int = 0
    
    
    var data = [["name":"会员基本信息","icon":"会员基本信息","iden":"UserBase2ViewController","isOn":"0"],
                ["name":"交易代表信息","icon":"交易代表","iden":"UserBase2ViewController","isOn":"0"],
                ["name":"银行账号信息","icon":"银行","iden":"UserBase2ViewController","isOn":"0"],
                ["name":"关联会员信息","icon":"备注","subTitle":" ","isRequird":false,"key":"user_remark","value":"","type":0]]
    
    //其中，type为0、输入框，1、选择框，2、多选点击框，3、身份证，4、单图上传，5、多行文本输入
    //会员基本信息
    let userData = [[["title":"会员名称","subTitle":"必填","isRequird":true,"key":"name","value":"","type":0],
                     ["title":"曾用名","subTitle":"选填","isRequird":false,"key":"alias","value":"","type":0],
                     ["title":"会员性质","subTitle":"必填","isRequird":true,"key":"type","value":"","type":1],
                     ["title":"企业经营类型","subTitle":"必填","isRequird":true,"key":"business_type","value":"","type":2]],
                    
                    [["title":"企业法人姓名","subTitle":"必填","isRequird":true,"key":"corporate","value":"","type":0],
                     ["title":"法人代表身份证号码","subTitle":"选填","isRequird":true,"key":"identity_card","value":"","type":0],
                     ["title":"法人代表身份证附件","subTitle":"未上传","isRequird":true,"key":"valid_doc_a_atta,valid_doc_b_atta","value":"","type":3],
                     ["title":"手机号码","subTitle":"必填","isRequird":true,"key":"phone","value":"","type":0],
                     ["title":"联系电话","subTitle":"选填","isRequird":false,"key":"landline","value":"","type":0],
                     ["title":"所在地区","subTitle":"请选择","isRequird":true,"key":"province_id,city_id,county_id","value":"","type":1],
                     ["title":"详细地址","subTitle":"必填","isRequird":true,"key":"address","value":"","type":0],
                     ["title":"邮编","subTitle":"选填","isRequird":false,"key":"zip_code","value":"","type":0],
                     ["title":"电子邮箱","subTitle":"选填","isRequird":false,"key":"email","value":"","type":0],
                     ["title":"传真","subTitle":"选填","isRequird":false,"key":"fox","value":"","type":0]],
                    [["title":"证件类型","subTitle":"请选择","isRequird":false,"key":"biz_license_type","value":"","type":1],
                     ["title":"证件编号","subTitle":"请填写","isRequird":false,"key":"biz_license_no","value":"","type":0],
                     ["title":"证件附件","subTitle":"未上传","isRequird":false,"key":"biz_license_atta","value":"","type":4],
                     ["title":"证件有效期","subTitle":"请选择","isRequird":false,"key":"biz_license_begin_at,biz_license_end_at","value":"","type":1]],
                    
                    [["title":"注册资金(万元)","subTitle":"万元","isRequird":false,"key":"registered_capital","value":"","type":0],
                     ["title":"税务证编号","subTitle":"请填写","isRequird":false,"key":"tax_cert_no","value":"","type":0],
                     ["title":"税务证附件","subTitle":"未上传","isRequird":false,"key":"tax_cert_atta","value":"","type":4],
                     ["title":"组织机构代码","subTitle":"请填写","isRequird":false,"key":"org_code","value":"","type":0],
                     ["title":"组织机构代码附件","subTitle":"未上传","isRequird":false,"key":"org_code_atta","value":"","type":4],
                     ["title":"加工许可证号","subTitle":"请填写","isRequird":false,"key":"processing_license_no","value":"","type":0],
                     ["title":"加工许可证附件","subTitle":"未上传","isRequird":false,"key":"processing_license_atta","value":"","type":4],
                     ["title":"收购许可证","subTitle":"请填写","isRequird":false,"key":"purchase_license_no","value":"","type":0],
                     ["title":"收购许可证附件","subTitle":"未上传","isRequird":false,"key":"purchase_license_atta","value":"","type":4],
                     ],
                    [["title":"日加工原料能力（吨）","subTitle":"吨","isRequird":false,"key":"processing_daily_capacity","value":"","type":0],
                     ["title":"日出库能力（吨）","subTitle":"吨","isRequird":false,"key":"depot_daily_ability","value":"","type":0]]
                    ]
    
    //其中，type为0、输入框，1、选择框，2、多选点击框，3、身份证，4、单图上传，5、多行文本输入
    // 交易代表信息
    let daibiaoData = [
                    [["title":"交易代表姓名","subTitle":"必填","isRequird":true,"key":"user_name","value":"","type":0],
                     ["title":"手机号码","subTitle":(UIApplication.shared.delegate as! AppDelegate).user_name,"isRequird":true,"key":"user_phone","value":(UIApplication.shared.delegate as! AppDelegate).user_name,"type":0],
                     ["title":"法人代表授权书","subTitle":"未上传","isRequird":true,"key":"user_auth_letter_atta","value":"","type":4],
                     ["title":"身份证号码","subTitle":"必填","isRequird":true,"key":"user_identity_card","value":"","type":0],
                     ["title":"身份证附件","subTitle":"未上传","isRequird":true,"key":"user_valid_doc_a_atta,user_valid_doc_b_atta","value":"","type":3],
                     ["title":"电子邮箱","subTitle":"非必填","isRequird":false,"key":"user_email","value":"","type":0],
                     ["title":"联系电话","subTitle":"必填","isRequird":true,"key":"user_landline","value":"","type":0]]]
    
//    ["title":"登录代码","subTitle":"非必填","isRequird":false,"key":"user_code","value":"","type":0],
    
    //银行账号信息
    let bankData = [
        [["title":"开户行","subTitle":"请选择","isRequird":true,"key":"bank_id","value":"","type":1],
         ["title":"开户行全称","subTitle":"请选择","isRequird":true,"key":"bank_completely_id","value":"","type":1],
         ["title":"开户行相关资料或银行卡","subTitle":"未上传","isRequird":true,"key":"bank_attachments","value":"","type":4],
         ["title":"开户行所在地","subTitle":"请选择","isRequird":true,"key":"bank_province_id,bank_city_id,bank_county_id","value":"","type":1]],
         [["title":"开户名","subTitle":(UIApplication.shared.delegate as! AppDelegate).enterprise_name,"isRequird":false,"key":"bank_name","value":(UIApplication.shared.delegate as! AppDelegate).enterprise_name,"type":0],
         ["title":"银行账号","subTitle":"必填","isRequird":true,"key":"bank_account","value":"","type":0],
         ["title":"确认银行账号","subTitle":"必填","isRequird":true,"key":"bank_account_confirmation","value":"","type":0]]]
    //银行账号信息
    var guanlianData = [
        [["title":"备注","subTitle":" ","isRequird":true,"key":"user_remark","value":"","type":0]]]
    

    
    
    @IBOutlet weak var tableView: UITableView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "会员信息"
        tableView.rowHeight = 50;
        // Do any additional setup after loading the view.
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
        NetWorkRequest(.enterpriseinfo()) { (res) -> (Void) in
            if res["code"] as! String == "000000" {
                let data = JSON(res["data"]!)
                (UIApplication.shared.delegate as! AppDelegate).enterprise_name = data["enterprise_name"].stringValue                
            }
        }
    }
    
    func loadData() -> Void {
        NetWorkRequest(.enterpriseinfo()) { (res) -> (Void) in
            if res["code"] as! String == "000000" {
                let js = jsonFormater(data: res["data"] as Any).dictionaryValue
                
                
                self.enterprise_id = js["enterprise_id"]?.intValue ?? 0
                
                self.guanlianData = [
                    [["title":"备注","subTitle":" ","isRequird":true,"key":"user_remark","value":"\(self.enterprise_id)","type":0]]]
                
                var isOn:Bool = true
                
                self.labelNote.text = "温馨提示：尊敬的用户，您好！由于您未完善资料，将无法进行交易！"
                
                if js["base_is_trade"]?.intValue == 1 {
                    self.data[0] = ["name":"会员基本信息","icon":"会员基本信息","iden":"UserBase2ViewController","isOn":"1"]
                }else {
                    isOn = false
                }
                
                if js["user_is_trade"]?.intValue == 1 {
                   self.data[1] = ["name":"交易代表信息","icon":"交易代表","iden":"UserBase2ViewController","isOn":"1"]
                }else {
                    isOn = false
                }
                
                if js["bank_is_trade"]?.intValue == 1 {
                    self.data[2] = ["name":"银行账号信息","icon":"银行","iden":"UserBase2ViewController","isOn":"1"]
                }else {
                    isOn = false
                }
                
                if isOn == false {
                    self.labelNote.text = "温馨提示：尊敬的用户，您好！由于您未完善资料，将无法进行交易！"
                }else {
                    self.labelNote.text = "温馨提示：尊敬的用户，您好！由于您未通过实名认证，将无法进行交易！"
                
                    if js["is_ca"]?.intValue == 1 {
                        
                    }else {
                        isOn = false
                    }
                }
                if isOn {
                    self.labelNote.isHidden = true
                }
                
                self.tableView.reloadData()
            }
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return data.count
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UserBaseTableViewCell", for: indexPath)
        
        let jd = jsonFormater(data: data[indexPath.row])
        
        cell.imageView?.image = UIImage(named: jd["icon"].stringValue)
        cell.textLabel?.text = jd["name"].stringValue;                
        
        if jd["isOn"].intValue == 1 {
            cell.detailTextLabel?.text = "已完善"
        }else {
            cell.detailTextLabel?.text = "未完善"
        }
        
        if indexPath.row > 2 {
            cell.detailTextLabel?.text = ""
        }
        
        return cell;
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        if indexPath.row <= 3 {
            
            let postData = [userData,daibiaoData,bankData,guanlianData];
            let vc = R.storyboard.user.userBase2ViewController()
            vc?.title = (data[indexPath.row]["name"] as! String)
            vc?.datas = postData[indexPath.row]
            vc?.type = indexPath.row
            self.navigationController?.pushViewController(vc!, animated: true)
            return
        }else {
            
            if self.enterprise_id <= 0 {
                showMessage(message: "请先填写会员基本资料")
                return
            }
            
            let vc = InputViewController()
            vc.data = ["key":"user_remark","value":"\(self.enterprise_id)"]
            vc.title = "备注"
            
            vc.bl = { (res) -> Void in
                
            }
            self.navigationController?.pushViewController(vc, animated: true)
        }
        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
